import Cookies from "js-cookie";
import { message } from 'antd';

// 权限路由
export const authRoutes = [
    '/ticket/[...order]',
    '/hotel/[...order]',
    '/line/[...order]',
    '/car/[...order]',
    '/shopping_cart',
    '/shopping_cart/order',
    '/personal',
    '/personal/order',
    '/personal/feedback',
    '/personal/coupon',
    '/personal/comment',
];

export function isLogin (){
    const token = Cookies.get('token'),
          userInfo = JSON.parse(Cookies.get('userInfo') || '{}');
    if(token && userInfo?.id){
        return true
    }
    return false
}

// 处理需登录路由
export function userLogin(router) {
    const { route, asPath } = router,
          _isLogin = isLogin();
    if(authRoutes.includes(route) && !_isLogin){
        message.warn('请先登录');
        router.replace(`/login?redirect=${asPath}`);
        return false;
    }
    return true
}